package com.micromsg.basic.sys.controller;

import com.micromsg.common.core.ApiResult;
import com.micromsg.common.core.PageResult;
import com.micromsg.common.util.BindingResultUtil;
import com.micromsg.basic.sys.service.MessageService;
import com.micromsg.basic.sys.vo.entity.MessageEntityVO;
import com.micromsg.basic.sys.vo.request.MessageRequestVO;
import com.micromsg.basic.sys.vo.response.MessageResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 消息表
 *
 * @author yangyang
 * @date 2024/04/06
 **/
@Slf4j
@RestController
@RequestMapping("/api/v1/sys")
@Api(description = "消息表")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @ApiOperation("获取消息表分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/message", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<PageResult<MessageResponseVO>>> getMessagePageList(MessageRequestVO requestVO) {
        try {
            PageResult<MessageResponseVO> pageResult = this.messageService.queryMessagePageList(requestVO);
            return ApiResult.ok(pageResult);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("获取消息表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/message/{id}", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<MessageEntityVO>> getMessage(@PathVariable("id") String id) {
        try {
            MessageEntityVO messageEntityVO = this.messageService.getMessageEntityVO(id);
            return ApiResult.ok(messageEntityVO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("新增消息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/message", method = RequestMethod.POST)
    public ResponseEntity<ApiResult<Object>> addMessage(@RequestBody @Validated MessageEntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.messageService.addMessage(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("更新消息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/message", method = RequestMethod.PUT)
    public ResponseEntity<ApiResult<Object>> updateMessage(@RequestBody @Validated MessageEntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.messageService.updateMessage(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }


    @ApiOperation("删除消息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/message/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ApiResult<Object>> deleteMessage(@PathVariable("id") String id) {
        try {
            this.messageService.deleteMessage(id);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }
}
